** Table 9 Panel B **
global db "D:\Dropbox\unequal_gains\main_data"
global Section4 "D:\Dropbox\unequal_gains\QJE revision plan\analysis\section4_data"
global resultspath "D:\Dropbox\unequal_gains\QJE revision plan\analysis\clean_results"


use "$db/Important Price Datasets/RMS_markup_all_final.dta", clear

merge 1:1 product_module_code quality using "$Section4/analysis_main.dta"
keep if _merge==3

gen ltornqvist_markup=log(tornqvist_markup_index)
gen ltornqvist_mcost=RMS_ltornqvist_price_index_win-ltornqvist_markup
gen ltornqvist_markup_C=log(tornqvist_markup_C_index)
gen ltornqvist_mcost_C=RMS_ltornqvist_price_index_win-ltornqvist_markup_C

gen lces_markup=log(ces_markup_index)
gen lces_mcost=RMS_lces_price_index_win-lces_markup
gen lces_markup_C=log(ces_markup_C_index)
gen lces_mcost_C=RMS_lces_price_index_win-lces_markup_C

* Set up table
gen Description=" "
replace Description="Beta" if [_n]==1
replace Description="SE" if [_n]==2
replace Description="First Stage F" if [_n]==3
* note first stage F stat will be adjusted for clustering across product modules
replace Description="Cragg-Donald F" if [_n]==4
replace Description="N" if [_n]==5

* Col (1)
ivreg2 RMS_lces_price_index_win (RMS_avg_Dlog_realspending=inst_linageres_w) [aw=sqrt(RMS_avg_spending)], first cluster(product_module_code)
gen Col_1=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_1=`b' if Description=="Beta"
replace Col_1=`se' if Description=="SE"
replace Col_1=e(rkf) if Description=="First Stage F"
replace Col_1=e(cdf) if Description=="Cragg-Donald F"
replace Col_1=e(N) if Description=="N"

* Col (2) 
ivreg2 lces_markup (RMS_avg_Dlog_realspending=inst_linageres_w) [aw=sqrt(RMS_avg_spending)], first cluster(product_module_code)
gen Col_2=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_2=`b' if Description=="Beta"
replace Col_2=`se' if Description=="SE"
replace Col_2=e(rkf) if Description=="First Stage F"
replace Col_2=e(cdf) if Description=="Cragg-Donald F"
replace Col_2=e(N) if Description=="N"

* Col (3)
ivreg2 lces_markup (RMS_avg_Dlog_realspending=inst_allres) [aw=sqrt(RMS_avg_spending)], first cluster(product_module_code)
gen Col_3=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_3=`b' if Description=="Beta"
replace Col_3=`se' if Description=="SE"
replace Col_3=e(rkf) if Description=="First Stage F"
replace Col_3=e(cdf) if Description=="Cragg-Donald F"
replace Col_3=e(N) if Description=="N"

* Col (4) 
ivreg2 lces_markup (RMS_avg_Dlog_realspending=inst_allres) i.quality i.product_group [aw=sqrt(RMS_avg_spending)], ///
partial(i.quality i.product_group) first cluster(product_module_code)
gen Col_4=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_4=`b' if Description=="Beta"
replace Col_4=`se' if Description=="SE"
replace Col_4=e(rkf) if Description=="First Stage F"
replace Col_4=e(cdf) if Description=="Cragg-Donald F"
replace Col_4=e(N) if Description=="N"

* Col (5) 
ivreg2 lces_markup_C (RMS_avg_Dlog_realspending=inst_linageres_w) [aw=sqrt(RMS_avg_spending)], first cluster(product_module_code)
gen Col_5=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_5=`b' if Description=="Beta"
replace Col_5=`se' if Description=="SE"
replace Col_5=e(rkf) if Description=="First Stage F"
replace Col_5=e(cdf) if Description=="Cragg-Donald F"
replace Col_5=e(N) if Description=="N"

* Col (6)
ivreg2 lces_markup_C (RMS_avg_Dlog_realspending=inst_allres) [aw=sqrt(RMS_avg_spending)], first cluster(product_module_code)
gen Col_6=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_6=`b' if Description=="Beta"
replace Col_6=`se' if Description=="SE"
replace Col_6=e(rkf) if Description=="First Stage F"
replace Col_6=e(cdf) if Description=="Cragg-Donald F"
replace Col_6=e(N) if Description=="N"

* Col (7) 
ivreg2 lces_markup_C (RMS_avg_Dlog_realspending=inst_allres) i.quality i.product_group [aw=sqrt(RMS_avg_spending)], ///
partial(i.quality i.product_group) first cluster(product_module_code)
gen Col_7=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_7=`b' if Description=="Beta"
replace Col_7=`se' if Description=="SE"
replace Col_7=e(rkf) if Description=="First Stage F"
replace Col_7=e(cdf) if Description=="Cragg-Donald F"
replace Col_7=e(N) if Description=="N"

* Col (8) 
ivreg2 lces_mcost (RMS_avg_Dlog_realspending=inst_linageres_w) [aw=sqrt(RMS_avg_spending)], first cluster(product_module_code)
gen Col_8=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_8=`b' if Description=="Beta"
replace Col_8=`se' if Description=="SE"
replace Col_8=e(rkf) if Description=="First Stage F"
replace Col_8=e(cdf) if Description=="Cragg-Donald F"
replace Col_8=e(N) if Description=="N"

* Col (9)
ivreg2 lces_mcost_C (RMS_avg_Dlog_realspending=inst_linageres_w) [aw=sqrt(RMS_avg_spending)], first cluster(product_module_code)
gen Col_9=.
matrix b=e(b) 
local b: di %6.5f `=b[1,1]'
matrix var=e(V)
matrix sd = sqrt(var[1,1])
local se: di %6.5f `=sd[1,1]'
replace Col_9=`b' if Description=="Beta"
replace Col_9=`se' if Description=="SE"
replace Col_9=e(rkf) if Description=="First Stage F"
replace Col_9=e(cdf) if Description=="Cragg-Donald F"
replace Col_9=e(N) if Description=="N"

* Finalize table and save
order Descr Col*
keep Descr Col*
format Col* %6.3f
drop if [_n]>5
save "$resultspath\Table8b", replace
